package com.welsy.authoring.business
{
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.mxml.WebService;	
	import mx.rpc.AbstractOperation;
	import com.adobe.cairngorm.business.ServiceLocator;
	import mx.rpc.IResponder;
	import mx.rpc.AsyncToken;
	import flash.utils.setTimeout;	
	import com.welsy.authoring.vo.CourseVO;
	import mx.collections.XMLListCollection;
	import com.welsy.authoring.vo.CourseComponentsVO;			

	/**
	  * Delegate for question result saving
	  * @author Welsy.org
	  * @version	$Revision: 1.1 $
	 */
	public class SaveQuestionResponseDelegate
	{
		/**
		  * Initialization for the question result saving delegate
		  * @param responder The handler for server SOAP response
		  */
		public function SaveQuestionResponseDelegate( responder : IResponder )
		{		
			this.service = ServiceLocator.getInstance().getWebService("UserTestResultsService") as WebService;			
			this.responder = responder;
		}
										
		/**
		  * Execute a question response save operation
		  * @param userId The ID of the user who performed the test
		  * @param testId The ID which is referring to the currently approached test
		  * @param questionId The ID which identifies exactly the question whose result is being saved
		  * @param score The raw score given to the question
		  * @param percentage How much the user response has to be considered exact
		  * @param consultationTime The time it took to the user to complete the question
		  */
		public function saveQuestionResponse(userId : String, testId : String, questionId : String, answerId : String, score : String, percentage : String, consultationTime : int) : void
		{	
					
			var ao : AbstractOperation = service.getOperation("SaveQuestionResponse");
			ao.arguments.SaveQuestionResponseRequest.UserId = userId;
			ao.arguments.SaveQuestionResponseRequest.TestId = testId;
			ao.arguments.SaveQuestionResponseRequest.QuestionId = questionId;
			ao.arguments.SaveQuestionResponseRequest.AnswerId = answerId;
			ao.arguments.SaveQuestionResponseRequest.Score = score;
			ao.arguments.SaveQuestionResponseRequest.PercentageScore = percentage;
			ao.arguments.SaveQuestionResponseRequest.ConsultationTime = consultationTime;
			
			var token:AsyncToken = service.SaveQuestionResponse();
			// notify this command when the service call completes
			token.addResponder( responder );
		}	
		
		private var responder : IResponder;
		private var service : Object;
	}

}